package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.studyadmin.dto.CourseParam;
import com.studyadmin.dto.StuLearningSituation;
import com.studyadmin.entity.Chapter;
import com.studyadmin.entity.ChapterContent;
import com.studyadmin.entity.Course;
import com.studyadmin.mapper.ChapterMapper;
import com.studyadmin.mapper.CourseMapper;
import com.studyadmin.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-19
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {



    @Autowired(required = false)
    CourseMapper courseMapper;
    @Autowired(required = false)
    ChapterMapper chapterMapper;


    /**
     * 学生端课程封面
     * @return 查询结果
     */
    @Override
    public JsonUtil courseListSer() {
        JsonUtil jsonUtil = new JsonUtil();

        List<CourseParam> courseParams= courseMapper.courseList();

        if (courseParams.size() <= 0) {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(courseParams);
        }
        return jsonUtil;
    }

    /**
     * 教师端查看指定学生的学习情况
     * 数据格式
     *  tableData: [
     *         {
     *           date: 'HTML',
     *           family: [
     *             {
     *               name: 'HTML简介',
     *               state: '已完成',
     *               time: '2023-2-20',
     *             },
     *           ],
     *         }]
     * @param stuNum 学生学号
     * @return 上述格式数据
     */

    @Override
    public JsonUtil StuLearningSituationSer(String stuNum,String courseId) {
        JsonUtil jsonUtil = new JsonUtil();
        //所有课程信息

        QueryWrapper<StuLearningSituation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(" c.stu_num",stuNum )
                .eq(" b.course_id",Integer.parseInt(courseId));

        List<StuLearningSituation> stuLearningSituations =
                courseMapper.selSituation(queryWrapper,Integer.parseInt(courseId));
        jsonUtil.setCode(0);
        jsonUtil.setData(stuLearningSituations);
        System.out.println(stuLearningSituations);


        return jsonUtil;
    }

    @Override
    public JsonUtil StuLearningWorkSer(String stuNum, String courseId) {
        JsonUtil jsonUtil = new JsonUtil();
        //所有课程信息

        QueryWrapper<StuLearningSituation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(" c.stu_num",stuNum )
                .eq(" b.course_id",Integer.parseInt(courseId));

        List<StuLearningSituation> stuLearningSituations =
                courseMapper.selSituationMapper(queryWrapper,Integer.parseInt(courseId));
        jsonUtil.setCode(0);
        jsonUtil.setData(stuLearningSituations);
        System.out.println(stuLearningSituations);


        return jsonUtil;
    }

    /**
     * 课程进度
     * @param stuNum
     * @return
     */
    @Override
    public JsonUtil StuScheduleSer(String stuNum) {

        JsonUtil jsonUtil = new JsonUtil();
        List<CourseParam> courseP=new ArrayList<>();
        //已学习章节数
        int chapterCount=0;
        List<CourseParam> courseParams = courseMapper.courseList();

        for (CourseParam course:courseParams) {

            System.out.println("hshshhsh====="+course.getCourseId());
            QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id",course.getCourseId());
            int count=chapterMapper.selectCount(wrapper);

            //查询学习的章节（视频学习，视为完成）
            int countLearn=chapterMapper.countChapter(stuNum,course.getCourseId());
            chapterCount=chapterCount+countLearn;

            double schedule=new BigDecimal((float)countLearn/count).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            CourseParam courseParam = new CourseParam();
            courseParam.setSchedule(schedule*100);
            courseParam.setTitle(course.getTitle());
            courseParam.setCourseId(course.getCourseId());

            courseP.add(courseParam);


        }
        System.out.println("---------------------------"+chapterCount);

        if (courseParams.size() <= 0) {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setCount(chapterCount);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(courseP);
        }
        return jsonUtil;
    }
}
